package xp2p.svc.file;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import xp2p.adv.jxta.FileAdv;
import xp2p.svc.PublisherBase;
import xp2p.utils.RandomBytes;
import net.jxta.content.Content;
import net.jxta.content.ContentID;
import net.jxta.content.ContentService;
import net.jxta.content.ContentShare;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.FileDocument;
import net.jxta.document.MimeMediaType;
import net.jxta.id.IDFactory;

/**
 * The Class FilePublisher.
 */
public class FilePublisher extends PublisherBase<FileAdv>
{
	private HashMap<String, FileAdv> files = new HashMap<String, FileAdv>();
	private HashMap<String, ContentID> ids = new HashMap<String, ContentID>();
	private HashMap<String, List<ContentShare>> shares = new HashMap<String, List<ContentShare>>();
	private Object sync = new Object();
	public String ftype = "file";

	public synchronized void AddFile(File file)
	{
		AddFile(file.getName(), file);
	}

	@Override
	protected void afterPublish(FileAdv ad)
	{
		synchronized (sync)
		{
			List<ContentShare> contents = shares.get(ad.getName());
			for (ContentShare c : contents)
			{
				if (isPropagate())
				{
					getGroup().getDiscoveryService().remotePublish(
							c.getContentShareAdvertisement(), getLifetime());
				}
				try
				{
					getGroup().getDiscoveryService().publish(
							c.getContentShareAdvertisement(), getLifetime(),
							getLifetime());
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}

	}

	public void AddFile(String name, File file)
	{
		synchronized (sync)
		{
			if (files.containsKey(name))
			{
				return;
			}
			ContentID id = IDFactory.newContentID(getGroup().getPeerGroupID(),
					true, RandomBytes.getRandomBytes());
			File destination = file;
			ContentService svc = getGroup().getContentService();
			FileDocument fileDoc = new FileDocument(destination,
					MimeMediaType.AOS);
			Content content = new Content(id, null, fileDoc);
			List<ContentShare> ss = svc.shareContent(content);
			shares.put(name, ss);
			ids.put(name, id);
			FileAdv ad = (FileAdv) AdvertisementFactory
					.newAdvertisement(FileAdv.DocType);
			ad.setID(id);
			ad.setFType(ftype);
			ad.setName(name);
			getAdv().add(ad);			
		}
	}

	public synchronized void RemoveFile(String name)
	{
		synchronized (sync)
		{
			if (!files.containsKey(name))
			{
				return;
			}
			ContentID id  = ids.get(name);
			ContentService svc = getGroup().getContentService();
			svc.unshareContent(id);
			Set<FileAdv> advs = getAdv();
			FileAdv ad1 = null;
			for (FileAdv ad : advs)
			{
				if (ad.getName().compareTo(name) == 0 && ad.getFType().compareTo(ftype) == 0)
				{
					ad1 = ad;
					break;
				}
			}
			if (ad1 != null)
			{
				getAdv().remove(ad1);
			}
			files.remove(name);
			ids.remove(name);
			shares.remove(name);
		}
	}
}
